package com.itools.core.controller;

import com.itools.core.base.CommonResult;
import com.itools.core.log.Logable;
import com.itools.core.request.FmsBase64FileUploadRequest;
import com.itools.core.result.*;
import com.itools.core.service.FmsFileSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
/**
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-01 11:26
 */
@RestController
@Api("文件控制访问接口")
public class FmsFileSystemController {

    @Autowired
    private FmsFileSystemService fmsFileSystemService;

    /**
     * 申请文件上传token
     * 仅内部文件服务使用，不应该对外开放
     * @param businessType 文件类型
     * @param fromSystemId 上传的文件来自哪个子系统
     * @param needEncrypt 是否需加密，后续扩展使用，本期不使用，默认否.0：否 1：是
     * @param remark 描述
     * @return
     */
    @PostMapping("/apply/token")
    @Logable
    @ApiOperation(value = "申请文件上传token", notes = "申请文件上传token", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessType", value = "文件类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fromSystemId", value = "上传的文件来自哪个子系统，系统编号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "needEncrypt", value = "是否需加密，后续扩展使用，本期不使用，默认否.0：否 1：是", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "文件描述信息", required = false, dataType = "String", paramType = "query")
    })
    public CommonResult<String> applyUploadToken(@RequestParam(required = true, value = "businessType")String businessType,
                                                 @RequestParam(required = true, value = "fromSystemId")String fromSystemId,
                                                 @RequestParam(required = false, value = "needEncrypt") String needEncrypt,
                                                 @RequestParam(required = false, value = "remark")String remark) {
        return fmsFileSystemService.applyUploadToken(businessType, fromSystemId, needEncrypt, remark);
    }

    /**
     * 获取文件访问路径
     * @param uniqueId
     * @param expiredTime
     * @param maxAccessCount
     * @param type
     * @return
     */
    @PostMapping("/get/file/path")
    @Logable
    @ApiOperation(value = "获取文件访问路径", notes = "获取文件访问路径", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uniqueId", value = "文件的唯一ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "expiredTime", value = "有效时长，单位分钟", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "maxAccessCount", value = "最大访问次数", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "文件下载 download/展示 show", required = true, dataType = "String", paramType = "query")
    })
    public CommonResult<String> getFileAccessUrl(String uniqueId, Integer expiredTime, @RequestParam(defaultValue = "-1", required = false) int maxAccessCount, String type) {
        return fmsFileSystemService.getFileUrlByFileId(uniqueId, expiredTime, maxAccessCount, type);
    }

    /**
     * 下载文件
     * @param uniqueId 文件唯一id
     * @param fileAccessToken 文件的访问token
     * @param response
     */
    @ApiOperation(value = "下载文件", notes = "下载文件", httpMethod = "GET")
    @GetMapping("/get/file/{uniqueId}")
    @Logable
    public void downloadFile(@PathVariable String uniqueId,
                             @RequestParam("fileAccessToken") String fileAccessToken,
                             HttpServletResponse response) {
        fmsFileSystemService.downloadFile(fileAccessToken,response,uniqueId);
    }

    @GetMapping("/get/file/base64")
    @Logable
    @ApiOperation(value = "获取base64文件", notes = "获取base64文件", httpMethod = "GET")
    public CommonResult<String> getFileBase64(@RequestParam("uniqueId")String uniqueId){
        return fmsFileSystemService.getFileBase64(uniqueId);
    }

    /**
     * 此接口不对外开放，仅供内部微服务使用
     * @param base64Req
     * @return
     */
    @PostMapping("File/BaseSixFour")
    @Logable
    @ApiOperation(value = "文件上传Base64", notes = "文件上传Base64", httpMethod = "POST")
    public CommonResult<String> uploadFileBase64(@RequestBody FmsBase64FileUploadRequest base64Req) {
        return fmsFileSystemService.uploadFileBase64(base64Req);
    }

    /**
     * 下载zip压缩文件接口
     * @param uniqueIds 文件唯一ids
     * @param zipName 压缩文件名称
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "下载zip压缩文件接口", notes = "下载zip压缩文件接口", httpMethod = "GET")
    @Logable
    @RequestMapping(value = "/download/zip",method = RequestMethod.GET)
    public void downloadZipByUniqueIds(@RequestParam("uniqueIds") List<String> uniqueIds,
                                       @RequestParam(value = "zipName", required = false) String zipName,
                                       HttpServletResponse response) throws IOException {
        fmsFileSystemService.downloadZipByUniqueIds(uniqueIds,zipName,response);
    }

}